package com.lanou.controller;

import com.lanou.entity.Answer;
import com.lanou.service.AnswerService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AnswerController
 * @Description TODO
 * @Author chenzhaobao
 * @Date 2025年08月14日 10:21
 * @Version 1.0
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;

    @PostMapping
    public R insert(@RequestBody Answer answer) {
        int i = answerService.insert(answer);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id) {
        int i = answerService.deleteById(id);
        if (i > 0) {
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping
    public R update(@RequestBody Answer answer) {
        int i = answerService.updateById(answer);
        if (i > 0) {
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id) {
        Answer answer = answerService.selectById(id);
        if (answer != null) {
            return R.ok(answer);
        }else{
            return R.fail("查询失败");
        }

    }
    @GetMapping
    public R selectAll() {
        List<Answer> answers = answerService.selectAll();
        if (answers != null && answers.size() > 0) {
            return R.ok(answers);
        }else{
            return R.fail("查询失败");
        }
        }
        @GetMapping("/consultant/{consultantId}")
        public R selectByConsultantId(@PathVariable Integer consultantId) {
        List<Answer> answers = answerService.selectByConsultantId(consultantId);
        if (answers != null && answers.size() > 0) {
            return R.ok(answers);
        }else{
            return R.fail("查询失败");
        }
        }
    }

